package hot;


import java.util.Deque;
import java.util.LinkedList;

/**
 * @author lakersUp
 * @Classname
 * @Description
 * @Date 2022/3/9 11:01
 * @Version 1.0
 */
public class Hot32longestValidParentheses {
    public int longestValidParentheses(String s) {
        int max=0;
        Deque<Integer> stack = new LinkedList<>();
        stack.push(-1);
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)=='('){
                stack.push(i);
            }else {
                stack.pop();
                if(stack.isEmpty()){
                    stack.push(i);
                }else {
                    max=Math.max(max,i-stack.peek());
                }
            }
        }
        return max;
    }
}
